<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION); 
   String token = (String) st.getValue(FormFields.TOKEN);
   boolean parent = (st.getValue("parent") != null);
   String name = (String) st.getValue(SchoolDBSQL.USRFNAME) + " " + (String) st.getValue(SchoolDBSQL.USRLNAME);
   ClassContainer classC[] = (ClassContainer[]) st.getValue("classContainer");
   UserContainer teacherContainer = (UserContainer) st.getValue("teacherContainer");
   int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
   int hwnum = (new Integer((String) st.getValue(FormFields.HWNUM))).intValue();
   HomeworkContainer homeworkC[] = (HomeworkContainer[]) st.getValue("homeworkContainer");
   String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
   String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
   AttachmentContainer attachmentC[];
   if (hwnum > -1) {
        attachmentC = homeworkC[hwnum].getAttachments();
   }
   else {
        attachmentC = new AttachmentContainer[] {};
   }
   String indentation = "&nbsp;&nbsp;&nbsp;&nbsp;";
%>


<HTML>
  <HEAD>
    <SCRIPT language="JavaScript" src="../jsp/calendar2.jsp"></SCRIPT>
    <SCRIPT language="JavaScript">
    
    function parseDeleteAttachments() {
        var i = 0;
        var returnList = "";
        var first = true;
        while (document.forms[0]["<%= FormFields.DELATTACHMENT %>" + i] != null) {
            if (document.forms[0]["<%= FormFields.DELATTACHMENT %>" + i].checked) {
                if (first) {
                    returnList = returnList + document.forms[0]["<%= FormFields.DELATTACHMENT %>" + i].value;
                    first = false;
                }
                else {
                    returnList = returnList + "," + document.forms[0]["<%= FormFields.DELATTACHMENT %>" + i].value;
                }
            }
            i += 1;
        }
        document.forms[0].<%= FormFields.DELATTACHMENT %>.value = returnList;   
        document.forms[0].<%= FormFields.LINKCLICKED %>.value = "Save";
    }
    
    function validateDates() {
        var dateAssigned = document.forms[0].<%= SchoolDBSQL.HWDATEASSIGNED %>.value;
        var dateDue = document.forms[0].<%= SchoolDBSQL.HWDATEDUE %>.value;

        var dateFormat = /^[0-1][0-9]\/[0-3][0-9]\/[0-9][0-9][0-9][0-9]$/;

        if (!dateAssigned.match(dateFormat) || Date.parse(dateAssigned) < 0) {
            alert("\""+dateAssigned + "\" is not a date in the the format of mm/dd/yyyy.\nPlease change the date and resubmit the assignment.");        
            document.forms[0].<%= SchoolDBSQL.HWDATEASSIGNED %>.focus();
            return false;
        }
        else if (!dateDue.match(dateFormat) || Date.parse(dateDue) < 0) {
            alert("\""+dateDue + "\" is not a date in the format of mm/dd/yyyy.\nPlease change the date and resubmit the assignment.");
            document.forms[0].<%= SchoolDBSQL.HWDATEDUE %>.focus();
            return false;
        }
        else if (Date.parse(dateAssigned) >= Date.parse(dateDue)) {
            alert("The assigned date must be prior to the due date.");
            return false;
        }
        else {
            return true;
        }
    }
    
    function postProcessing(action) {
        parseDeleteAttachments();
        if (validateDates()) {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = action;
            document.forms[0].submit();
        }

    }
    
    </SCRIPT>
    
  </HEAD>
  <BODY>
<!--    <FORM NAME="classinfoform" METHOD="POST"> -->
    <FORM NAME="classinfoform" METHOD="POST" >
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= token %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%=loginType %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.HWASSIGNEDID %>" VALUE="<%= ((hwnum == -1) ? "-1" : homeworkC[hwnum].getHomeworkID().toString()) %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.DELATTACHMENT %>" VALUE="">

    <% if (hwnum == -1 && classnum != -2) {
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CLASSASSIGNEDID + "\" VALUE=\"" + classC[classnum].getClassAssignedID().toString() + "\">");
        out.println("<TABLE STYLE=\"font-family:sans-serif;font-size:12;\">");
        out.println("<TR>");
        out.println("<TD WIDTH=\"49\"><B>Homework:&nbsp;&nbsp;</B>" + indentation + "</TD><TD WIDTH=\"200\"><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWSHORTNAME + "\" VALUE=\"\"></TD>");
        out.println("<TD><B>Points:&nbsp;&nbsp;</B>" + indentation + "</TD><TD><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWTOTALPOINTS + "\" VALUE=\"\"></TD>");
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Date assigned:&nbsp;&nbsp;</B>" + indentation + "</TD><TD><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWDATEASSIGNED + "\" SIZE=\"9\" VALUE=\"\">&nbsp;");

        out.println("<noPrint><a href=\"javascript:calAssigned.popup();\"><img src=\"../pics/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;(mm/dd/yyyy)</noPrint></TD><br>");

        out.println("<TD><B>Date due:&nbsp;&nbsp;</B>" + indentation + "</TD><TD><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWDATEDUE + "\" SIZE=\"9\" VALUE=\"\">&nbsp;");

        out.println("</noPrint><a href=\"javascript:calDue.popup();\"><img src=\"../pics/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;(mm/dd/yyyy)</noPrint></TD><br>");

        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Add Attachment:&nbsp;&nbsp;</B>" + indentation + "</TD><TD COLSPAN=\"1\"><input type=\"file\" NAME=\"" + SchoolDBSQL.ATTFILENAME + "\" VALUE=\"\"></TD>");
        out.println("<TD><B>Allow online submissions: </B>" + indentation + "</TD><TD><input type=\"checkbox\" NAME=\"" + SchoolDBSQL.HWSUBMISSIONSALLOWED + "\" VALUE=\"TRUE\"></TD>");
        out.println("</TR>");
        out.println("<TR VALIGN=\"CENTER\">");
        out.println("<TD><B>Description:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"3\"><TEXTAREA STYLE=\"font-family:sans-serif;font-size:12;\" ROWS=\"10\" COLS=\"71\" NAME=\"" + SchoolDBSQL.HWDESCRIPTION + "\"></TEXTAREA></TD>");
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Notes:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"3\"><TEXTAREA STYLE=\"font-family:sans-serif;font-size:12;\" ROWS=\"10\" COLS=\"71\" NAME=\"" + SchoolDBSQL.HWNOTES + "\"></TEXTAREA></TD>");
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Answer:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"3\"><TEXTAREA STYLE=\"font-family:sans-serif;font-size:12;\" ROWS=\"10\" COLS=\"71\" NAME=\"" + SchoolDBSQL.HWANSWERS + "\"></TEXTAREA></TD>");
        out.println("</TR>");
        out.println("</TABLE>");
//        out.println("<CENTER><INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Assign\" onClick=\"postProcessing()\">" + indentation);
        out.println("<CENTER><INPUT TYPE=\"BUTTON\" NAME=\"submitbutton\" VALUE=\"Assign\" onClick=\"postProcessing('Assign')\">" + indentation);
//        out.println("<CENTER><INPUT TYPE=\"SUBMIT\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Assign\">" + indentation);
        out.println("<INPUT TYPE=\"SUBMIT\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Cancel\"></CENTER>");
    }
    else if( hwnum != -1){
//      if (classnum == -2) {
            out.println("<FONT STYLE=\"font-family:sans-serif;font-size:12;\">");
            out.println("<B>");
            out.println(ClassContainer.getClassContainer(homeworkC[hwnum].getClassID().toString(), schoolName).getAssociatedCourse().getCourseShortName().toString() + " ");
            out.println("<BR>");
            out.println("</B>");
            out.println("</FONT>");
//          }
        out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + SchoolDBSQL.CLASSASSIGNEDID + "\" VALUE=\"" + homeworkC[hwnum].getClassID().toString() + "\">");
        out.println("<TABLE STYLE=\"font-family:sans-serif;font-size:12;\">");
        out.println("<TR>");
        out.println("<TD><B>Homework:&nbsp;&nbsp;</B>" + indentation + "</TD><TD WIDTH=\"200\"><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWSHORTNAME + "\" VALUE=\"" + homeworkC[hwnum].getShortName().toString() + "\"></TD>");
        out.println("<TD><B>Points:&nbsp;&nbsp;</B>" + indentation + "</TD><TD><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWTOTALPOINTS + "\" VALUE=\"" + homeworkC[hwnum].getTotalPoints().toString() + "\"></TD>");
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Date assigned:&nbsp;&nbsp;</B>" + indentation + "</TD><TD><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWDATEASSIGNED + "\" VALUE=\"" + homeworkC[hwnum].getDateAssigned().toString() + "\" SIZE=\"9\">&nbsp;");

        out.println("<a href=\"javascript:calAssigned.popup();\"><img src=\"../pics/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;(mm/dd/yyyy)</TD><br>");

        out.println("<TD><B>Date due:&nbsp;&nbsp;</B>" + indentation + "</TD><TD><INPUT STYLE=\"font-size:12;\"; TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.HWDATEDUE + "\" VALUE=\"" + homeworkC[hwnum].getDateDue().toString() + "\" SIZE=\"9\">&nbsp;");

        out.println("<a href=\"javascript:calDue.popup();\"><img src=\"../pics/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;(mm/dd/yyyy)</TD><br>");

        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Add Attachment:&nbsp;&nbsp;</B>" + indentation + "</TD><TD COLSPAN=\"1\"><input type=\"file\" name=\"" + SchoolDBSQL.ATTFILENAME + "\"  VALUE=\"\"></TD>");
        out.println("<TD><B>Allow online submissions: </B>" + indentation + "</TD><TD><input type=\"checkbox\" NAME=\"" + SchoolDBSQL.HWSUBMISSIONSALLOWED + "\" VALUE=\"TRUE\"" + homeworkC[hwnum].getSubmissionsAllowed().getCheckedString() + "></TD>");
        out.println("</TR>");
        if (attachmentC.length != 0) {
            out.println("<TR>");
            out.println("<TD><B>Attachments:&nbsp;&nbsp;</B>" + indentation + "</TD><TD COLSPAN=\"3\"><TABLE STYLE=\"font-family:sans-serif;font-size:12;\"><TR><TD>&nbsp;</TD><TD>Check files to delete</TD></TR>");
            for (int i = 0; i < attachmentC.length; i++) {
            out.print("<TR><TD><a href=\"..\\" + attachmentC[i].getFilePath().toString()+ attachmentC[i].getFileName().toString() + "\" target='4087996' onClick=\"window.open('','4087996','width=640,height=480,resizeable=1,status=1,scrollbars=1,menubar=1');\"><IMG src=\"../pics/paperclip.gif\" width=25 border=0> " + attachmentC[i].getFileName().toString() + "  (" + attachmentC[i].getFileSize().toString() + " bytes)</a></TD>");
            out.println("<TD><INPUT TYPE=\"CHECKBOX\" NAME=\"" + FormFields.DELATTACHMENT + i + "\" VALUE=\"" + attachmentC[i].getAssignedAttachmentID().toString() + "\"></TD></TR>");
            }
            out.println("</TABLE></TD></TR>");
        }
        out.println("<TR>");
        out.println("<TD><B>Description:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"3\"><TEXTAREA  STYLE=\"font-family:sans-serif;font-size:12;\" ROWS=\"10\" COLS=\"71\" NAME=\"" + SchoolDBSQL.HWDESCRIPTION + "\">" + homeworkC[hwnum].getDescription().toString() + "</TEXTAREA><TD>");
        String notes = homeworkC[hwnum].getNotes().toString();
        if (notes == null) {
            notes = "";
        }
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Notes:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"3\"><TEXTAREA STYLE=\"font-family:sans-serif;font-size:12;\" ROWS=\"10\" COLS=\"71\" NAME=\"" + SchoolDBSQL.HWNOTES + "\">" + notes + "</TEXTAREA><TD>");
        String answers = homeworkC[hwnum].getAnswers().toString();
        if (answers == null) {
            answers = "";
        }
        out.println("</TR>");
        out.println("<TR>");
        out.println("<TD><B>Answer:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"3\"><TEXTAREA STYLE=\"font-family:sans-serif;font-size:12;\" ROWS=\"10\" COLS=\"71\" NAME=\"" + SchoolDBSQL.HWANSWERS + "\">" + answers + "</TEXTAREA><TD>");
        out.println("</TR>");
        out.println("</TABLE>");
//        out.println("<CENTER><INPUT TYPE=\"SUBMIT\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Save\">" + indentation);
//        out.println("<CENTER><INPUT TYPE=\"BUTTON\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Save\" onClick=\"postProcessing()\">" + indentation);
        out.println("<CENTER><INPUT TYPE=\"BUTTON\" NAME=\"submitbutton\" VALUE=\"Save\" onClick=\"postProcessing('Save')\">" + indentation);
        out.println("<INPUT TYPE=\"SUBMIT\" NAME=\"" + FormFields.BUTTONCLICKED + "\" VALUE=\"Cancel\"></CENTER>");

    }
    %>
    <script language="JavaScript">
    <!-- // create calendar object(s) just after form tag closed
         // specify form element as the only parameter (document.forms['formname'].elements['inputname']);
         // note: you can have as many calendar objects as you need for your application
        var calAssigned = new calendar2(document.forms['classinfoform'].elements['<%= SchoolDBSQL.HWDATEASSIGNED %>']);
        calAssigned.year_scroll = false;
        calAssigned.time_comp = false;
        var calDue = new calendar2(document.forms['classinfoform'].elements['<%= SchoolDBSQL.HWDATEDUE %>']);
        calDue.year_scroll = false;
        calDue.time_comp = false;
    //-->
    </script>
    </FORM>
  </BODY>
</HTML>